package com.gy.paidao.main;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiItemDetail;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.gy.paidao.BaseActivity;
import com.gy.paidao.MyApplication;
import com.gy.paidao.R;
import com.gy.paidao.main.adapter.SearchAdapter;
import com.gy.paidao.util.AnimUtil;
import com.gy.paidao.util.LogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author GY
 * @date 16/4/21
 * @Description:
 */
public class MapSearchActivity extends BaseActivity implements PoiSearch.OnPoiSearchListener,
        AdapterView.OnItemClickListener{

    private ImageView imgBcak;
    private TextView tvCancel;
    private EditText etLoc;
    private ListView listView;
    private List<PoiItem> mDatas,mSearchDatas;
    private SearchAdapter mAdapter;
    private PoiSearch poiSearch;// 周边搜索
    private PoiSearch.Query query;// 周边搜索条件
    private PoiResult poiResult;// 搜索结果
    public final static int POI_DISTANCE = 10000;// 附近点距离

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_search);
        setViews();
        startSearch("");
    }

    @Override
    protected void initVariable() {
        mDatas = new ArrayList<>();
        mSearchDatas = new ArrayList<>();
        mAdapter = new SearchAdapter(this,mDatas);
    }

    @Override
    protected void setTitleViews() {

    }

    @Override
    protected void setViews() {
        imgBcak = (ImageView) findViewById(R.id.map_search_img_back);
        tvCancel = (TextView) findViewById(R.id.map_search_tv_cancel);
        etLoc = (EditText) findViewById(R.id.map_search_et_loc);
        listView = (ListView) findViewById(R.id.map_search_listview);

        imgBcak.setOnClickListener(this);
        tvCancel.setOnClickListener(this);
        listView.setAdapter(mAdapter);
        listView.setOnItemClickListener(this);
        etLoc.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                startSearch(s.toString());
            }
        });
    }

    @Override
    protected void doClickAction(int viewId) {
        super.doClickAction(viewId);
        switch (viewId){
            case R.id.map_search_img_back:
                this.finish();
                AnimUtil.pushRightInAndOut(this);
                break;
            case R.id.map_search_tv_cancel:
                etLoc.setText("");
                break;
        }
    }

    /** 设置周边搜索条件*/
    private void startSearch(String keyWord) {
        query = new PoiSearch.Query(keyWord, "", null);
        query.setPageNum(0);// 设置查第一页
        query.setPageSize(20);
        poiSearch = new PoiSearch(this, query);
        poiSearch.setOnPoiSearchListener(this);
        poiSearch.setBound(new PoiSearch.SearchBound(new LatLonPoint(Double.valueOf(MyApplication.CURRENT_Y),
                Double.valueOf(MyApplication.CURRENT_X)), POI_DISTANCE, true));//设置收缩条件
        poiSearch.searchPOIAsyn();//开始搜索
    }

    @Override
    public void onPoiSearched(PoiResult result, int rCode) {
        if (rCode == 0) {
            // 搜索POI的结果
            if (result != null && result.getQuery() != null) {
                // 是否是同一条
                if (result.getQuery().equals(query)) {
                    poiResult = result;
                    // 取得搜索到的poiitems有多少页
                    int resultPages = poiResult.getPageCount();
                    // 取得第一页的poiitem数据，页数从数字0开始
                    List<PoiItem> poiItems = poiResult.getPois();
                    // 当搜索不到poiitem数据时，会返回含有搜索关键字的城市信息
                    List suggestionCities = poiResult
                            .getSearchSuggestionCitys();
                    if (poiItems != null && poiItems.size() > 0) {
                        mDatas.clear();
                        mDatas.addAll(poiItems);
                        mAdapter.notifyDataSetChanged();
                    } else if (suggestionCities != null
                            && suggestionCities.size() > 0) {
                        // 没有搜索到数据
                        showToast("很遗憾，附近没有搜索结果！");
                    } else {
                        // 没有搜索到数据
                        showToast("很遗憾，附近没有搜索结果！");
                    }
                }
            } else {
                showToast("很遗憾，附近没有搜索结果！");
            }
        } else {
            showToast("对不起，请稍后重试!");
        }
    }

    @Override
    public void onPoiItemDetailSearched(PoiItemDetail poiItemDetail, int i) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String result = mDatas.get(position).getTitle();
        Intent intent = new Intent();
        intent.putExtra("loc",result);
        setResult(105,intent);
        this.finish();
    }
}
